package com.atguigu.gmall.pms.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
@Slf4j
@Configuration
public class MqConfig {
    @Autowired
    RabbitTemplate rabbitTemplate;
    //设置生产者回调接口
    @PostConstruct
    public void init(){
        //消息未到达交换机的回调
        rabbitTemplate.setConfirmCallback((correlationData,ack,cause)->{
            if(!ack){
                log.error("消息未到达交换机:{}"+cause);
            }
        });
        //消息未到达队列时的回调
        rabbitTemplate.setReturnCallback((Message message, int replyCode, String replyText, String exchange, String routingKey)->{
            log.error("消息未到达队列："+exchange+" , 路由key: "+ routingKey+
                    ", 应答码："+ replyCode+" , 原因："+ replyText+ " , 消息："+ new String(message.getBody()));
        });
    }
}
